# _*_ coding: UTF-8 _*_
# 2020/5/6 21:06 
# PyCharm  
# Create by:LIUMINXUAN
# E-mail:liuminxuan1024@gmail.com
import cv2 as cv
import numpy as np
from scipy import signal


def roberts(I, _boundary='fill', _fillvalue=0):
    H1, W1 = I.shape[0:2]
    H2, W2 = 2, 2
    R1 = np.array([[1, 0], [0, -1]], np.float32)
    kr1, kc1 = 0, 0
    IconR1 = signal.convolve2d(I, R1, mode='full', boundary=_boundary, fillvalue=_fillvalue)
    IconR1 = IconR1[H2 - kr1 - 1:H1 + H2 - kr1 - 1, W2 - kc1 - 1:W1 + W2 - kc1 - 1]
    R2 = np.array([[0, 1], [-1, 0]], np.float32)
    IconR2 = signal.convolve2d(I, R2, mode='full', boundary=_boundary, fillvalue=_fillvalue)
    kr2, kc2 = 0, 1
    IconR2 = IconR2[H2 - kr2 - 1:H1 + H2 - kr2 - 1, W2 - kc2 - 1:W1 + W2 - kc2 - 1]
    return IconR1, IconR2


if __name__ == "__main__":
    image = cv.imread("./images/yuantu/danghui_yuantu.bmp", cv.IMREAD_GRAYSCALE)
    cv.imshow("image", image)
    IconR1, IconR2 = roberts(image, 'symm')
    IconR1 = np.abs(IconR1)
    edge_45 = IconR1.astype(np.uint8)
    cv.imshow("edge_45", edge_45)
    IconR2 = np.abs(IconR2)
    edge_135 = IconR2.astype(np.uint8)
    cv.imshow("edge_45", edge_135)
    edge = np.sqrt(np.power(IconR1, 2.0) + np.power(IconR2, 2.0))
    edge = np.round(edge)
    edge[edge > 255] = 255
    edge = edge.astype(np.uint8)
    cv.imshow("edge", edge)
    cv.waitKey(0)
    cv.destroyAllWindows()
